/**
 * @param {string} haystack
 * @param {string} needle
 * @return {number}
 */
 var strStr = function(haystack, needle) {
    if(!needle){
        return 0
    }

    const count=haystack.length-needle.length+1;

    for(let i=0;i<count;i++){
        if(haystack[i]===needle[0]){
            if(isSame(i,i+needle.length-1)){
                return i
            }
        }
        
    }
    return -1

    function isSame(start,end){
        let index=start
        while(index<=end){
            if(haystack[index]!==needle[index-start]){
                return false
            }
            index++
        }
        return true
    }
};

// 使用api
var strStr = function(haystack, needle) {
    if(!needle){
        return 0
    }

    const count=haystack.length-needle.length+1;

    for(let i=0;i<count;i++){
        if(haystack.slice(i,i+needle.length)===needle){
            return i
        }
    }
    return -1
};
